package com.wf.course.web.controller.course;

import com.utils.page.PageBean;
import com.wf.course.model.ad.VipCard;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.model.course.CourseGift;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.service.ICourseGiftService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.service.IVipCardService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther: liu
 * @Date: 2018/6/11 16:56
 * @Description: 课程赠送功能
 */
@Controller
@RequestMapping("course/gift")
public class CourseGiftController {

    private static final Logger logger = LoggerFactory.getLogger(CourseGiftController.class);

    @Autowired
    private ICourseGiftService courseGiftService;

    @Autowired
    private ICourseInfoService courseInfoService;

    @Autowired
    private ICourseChapterService courseChapterService;

    @Autowired
    private IVipCardService vipCardService;

    @RequestMapping("list")
    public String list(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                       Model model, CourseGift courseGift) {
        PageBean<CourseGift> pageBean = new PageBean<>(pageNo, pageSize);
        pageBean.setUrl("course/gift/list");
        pageBean.setObj(courseGift);
        courseGiftService.list(pageBean);
        model.addAttribute("page", pageBean);
        return "course/courseGiftList";
    }

    @RequestMapping("pageLoad/{id}")
    public String toAdd(@PathVariable("id") Integer id, Model model) {
        List<CourseInfo> courses = courseInfoService.queryCourses();
        model.addAttribute("courses", courses);

        List<CourseChapter> chapters = courseChapterService.queryCourseChapters();
        model.addAttribute("chapters", chapters);

        List<VipCard> cards = vipCardService.queryVipCards();
        model.addAttribute("cards", cards);
        if (!id.equals(0)) {
            model.addAttribute("giftInfo", courseGiftService.getGiftInfoById(id));
        }
        return "course/courseGiftEdit";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(CourseGift courseGift) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String[] time = null;
        try {
            if (StringUtils.isNotBlank(courseGift.getTime())) {
                time = courseGift.getTime().split(" - ");
                courseGift.setStartTime(format.parse(time[0]).getTime());
                courseGift.setEndTime(format.parse(time[1]).getTime());
            }
            courseGift.setCreateTime(System.currentTimeMillis());
            switch (courseGift.getType()) {
                case 1:
                    courseGift.setCourseId(courseGift.getCourseIds());
                    break;
                case 2:
                    courseGift.setCourseId(courseGift.getChapterIds());
                    break;
                case 3:
                    courseGift.setCourseId(courseGift.getCardIds());
                    break;
            }
            switch (courseGift.getGiftType()) {
                case 1:
                    courseGift.setGiftCourseId(courseGift.getGiftCourseIds());
                    break;
                case 2:
                    courseGift.setGiftCourseId(courseGift.getGiftChapterIds());
                    break;
                case 3:
                    courseGift.setGiftCourseId(courseGift.getGiftCardIds());
                    break;
            }
            if (courseGift.getId() != null) {
                courseGiftService.update(courseGift);
            } else {
                courseGiftService.add(courseGift);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "redirect:/course/gift/list";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        CourseGift courseGift = new CourseGift();
        courseGift.setId(id);
        courseGiftService.delete(courseGift);
        return "redirect:/course/gift/list";
    }
}
